<?php
/*************************************************************************
Avasoft CMS Project
Company: Avasoft Co., Ltd                                  
Name: Tran Thi Kim Que
Last updated: 08/03/2011
**************************************************************************/
//error_reporting  (E_ERROR | E_WARNING | E_PARSE) ;
//ini_set('display_errors','1'); ini_set('display_startup_errors','1'); 
include_once(ROOT_PATH."plugins/authimg/authimg.php");
include_once(ROOT_PATH."includes/functions.php");
include_once(ROOT_PATH."classes/dao/members.class.php");
include_once("class.phpmailer.php");
include_once("class.smtp.php");
include_once(ROOT_PATH."classes/dao/configs.class.php");
$configs = new Configs();
$members = new Members();
$templateFile = "register.tpl.html";
$token = $request->element('token','');
$username =$request->element('username','');
$password =$request->element('password','');
$fullname =$request->element('fullname','');
$birthday=$request->element('birthday','');
$email = $request->element('email','');
$country = $request->element('country','');
$company = $request->element('company','');
$tinh = $request->element('city','');
$phai = $request->element('gender','');
$address = $request->element('address','');
$tel = $request->element('tel','');
$fax = $request->element('fax','');
$cel = $request->element('cel','');
$chk_service= $request->element('chk_service');
$chk_product= $request->element('chk_product');
$chk_visistor= $request->element('chk_visistor');
$chk_project= $request->element('chk_project');
$chk_isvietcons= $request->element('chk_isvietcons');
$chk_ispartner= $request->element('chk_ispartner');
$pageName = "Đăng ký thành viên";
$template->assign('pageName',$pageName);
$property=array(
    'chk_service'=>$chk_service,
    'chk_product'=>$chk_product,
    'chk_visistor'=>$chk_visistor,
    'chk_project'=>$chk_project,
    'chk_isvietcons'=>$chk_isvietcons,
    'chk_ispartner'=>$chk_ispartner
    );
$properties= serialize($property);
$comment=$messages["register_email_body"];
if(!empty($token)) {
    $templateFile = "active_account.tpl.html";
    $dToken = base64_decode($token);
    $arrDToken = explode("/", $dToken);
    $emailActive = $arrDToken[0];
    $passActive = $arrDToken[1];
    $infoMember = $members->getRecordFromEmail($emailActive);
    if(!empty($infoMember) && ($passActive == substr($infoMember->getPass(), 1, strlen($infoMember->getPass())/2))) {
        $members->updateData(
                    array(
                        'status'=>1
                    ),
                    $infoMember->getId(),
                    'id'
                );
		$error = "Chúc mừng bạn. Tài khoản của bạn đã được kích hoạt thành công.";
		$template->assign("error",$error);
        
    }else{
		$error = "Tài khoản của bạn không tồn tại. Vui lòng kiếm trai lại";
		$template->assign("error",$error);
	}
}  elseif($_POST){
		include_once(ROOT_PATH."classes/data/validate.class.php");
		 $validate = validateData($request);
		 if($validate == '') {
			 $passmd5 = md5($password);
				$member = new MemberInfo($username,$passmd5,0,$fullname,$birthday,"",$email,$country,$tinh,$company,$phai,$address,$tel,$fax,$cel,date("Y-m-d H:i:s"),date("Y-m-d H:i:s"),$status = 0,$properties,'');
				
				$fields = array('username'=>$member->getUsername(),
								'password'=>$member->getPass(),							  							    
								'fullname'=>$member->getFullname(),
								'type'=>$member->getType(),
							    'birthday'=>$member->getBirthday(),
								'about'=>$member->getAbout(),
							    'email'=>$member->getEmail(),
								'country'=>$member->getCountry(),
							  	'tinh'=>$member->getTinh(),
							   	'company'=>$member->getCompany(),
							   	'phai'=>$member->getPhai(),
							   	'address'=>$member->getAddress(),
								'tel'=>$member->getTel(),
								'fax'=>$member->getFax(),
								'cel'=>$member->getCel(),
							   	'date_created'=>$member->getDateCreted(),
							   	'last_login'=>$member->getLastLogin(),
							   	'status'=>$member->getStatus(),
							    'properties'=>$member->setProperties()
							    );
				//print_r($fields);
				$members->addData($fields);
				$admin_mail = "kque.tran@gmail.com";
				$company = "Công ty TNHH VIETCAD";
				$admin_mail = $siteConfigs->getProperty('admin_mail');
			$ip = $_SERVER["REMOTE_ADDR"];
			$user_agent = $_SERVER['HTTP_USER_AGENT'];
            $link = DOMAIN."dang-ky/token/".  
                    base64_encode($member->getEmail()."/".substr($member->getPass(), 1, strlen($member->getPass())/2)).'.html';
			$response="<br/>{$comment} <br/>
                Username: {$username} <br/>
                    Password: {$password} <br/>
                        Name: {$fullname} <br/>
                            Email: {$email} <br/>
                                Address: $address<br/>
                                    Vui lòng click vào link <a href=\"{$link}\">{$link}</a> để kích hoạt tài khoản của bạn.
                            ";
			//mail("info@mypapit.net","Contact form fakapster",$response, $headers);
			//
			$mail = new PHPMailer();
						$mail->IsSMTP(); // set mailer to use SMTP
						$mail->Host =SMTP_HOST; // specify main and backup server
						$mail->Port = SMTP_PORT; // set the port to use
						$mail->SMTPAuth = true; // turn on SMTP authentication
						#$mail->SMTPSecure = 'ssl';
						$mail->Username = SMTP_USER; // your SMTP username or your gmail username
						$mail->Password = SMTP_PASSWORD; // your SMTP password or your gmail password
						$from = $admin_mail; // Reply to this email
						$to=$email; // Recipients email ID
						$name=$company; // Recipient's name
						$mail->header = 'Content-Type: text/html; charset=utf-8'."\r\n";
						$mail->From = $from;
						$mail->FromName = $company; // Name to indicate where the email came from when the recepient received
						$mail->AddAddress($email,$email);
						$mail->AddReplyTo($email,$fullname);
						$mail->WordWrap = 50; // set word wrap
						$mail->IsHTML(true); // send as HTML
						$mail->Subject ="Thông tin đăng ký thành viên từ hệ thống website";
						$mail->Body = $response; //HTML Body
						//$mail->AltBody = "Mail nay duoc goi bang phpmailer class. - bloghoctap.com"; //Text Body
						//$mail->SMTPDebug = 2;
						$mail->Send(); //khong goi mail cho khach nua, sua theo email yeu cau cua Mr Thien 17/7/2012
						$mail->ClearAddresses();
						$mail->ClearAttachments();	
						unset($_SESSION['rand_code']);
						$error = $messages["register_ok"];
						$template->assign("error",$error);
						$template->assign("infoClass","info");
			
			
			
	}else{
		$error = $messages["register_error"]."<br />".$validate;
		$template->assign("error",$error);
		$template->assign("infoClass","info");
		$template->assign("username",$username);
		$template->assign("address",$address);	
		$template->assign("tel",$tel);
		$template->assign("email",$email);	
		unset($_SESSION['rand_code']);		
	}
}
function validateData($request) {
	global $messages;
	$members = new Members();
	$error = '';
		if($members->checkDuplicate($request->element('username'),"username"))
			$error .= $messages['duplicate_username'];
		if($members->checkDuplicate($request->element('email'),"email"))
			$error .= $messages['duplicate_email'];
		$validate = new Validate();
		$code = strtolower($request->element('code'));
		$error .= $validate->validString($messages['security'],$request->element('code'));	
		if(!isset($_SESSION['rand_code']) || $code != strtolower($_SESSION['rand_code'])) $error .= $messages["invalid_security_code"].'<br />';
	return $error;
}


$plus = $request->element('plus','');
if ($plus == 'apply'){ 
	 $addon = $request->element('addon','');
	$templateFile = "registerapply.temp.html";
	$user = $members->getRecordFromU($addon);
	if ($user){
		$id = $user->getId();
		$members->changeStatus($id ,1);
	}
}


#end product
include_once(ROOT_PATH."modules/framework.module.php");?>